<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="/resources/template/template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns="http://www.w3.org/1999/xhtml">



    <ui:define name="content">
        <h:panelGroup rendered="#{!webUserController.hasPrivilege('LabBilling')}" >
            You are NOT authorized
        </h:panelGroup>

        <h:panelGroup rendered="#{webUserController.hasPrivilege('LabBilling')}" styleClass="opdPanel" >
            <h:form id="form">
                <p:focus id="focusIx" for="acIx" />
                <p:panel style="min-height: 600px; position:relative;"  >
                    <f:facet name="header" >
                        <h:panelGrid columns="10"  >
                            <h:outputLabel value="Inward Service Bill" />
                            <p:commandButton  value="Save" action="#{inwardServiceBillController.save}" ajax="false"  >
                            </p:commandButton>
                            <p:commandButton value="New Bill" ajax="false" action="#{inwardServiceBillController.prepareNewBill()}" style="float: right;">
                            </p:commandButton>
                        </h:panelGrid>
                    </f:facet>

                    <h:panelGrid columns="2" >
                        <h:panelGrid columns="1" >
                            <h:panelGroup id="panelPatient" >

                                <p:panel header="Patient Details" style="min-height: 200px;">

                                    <h:outputLabel value="Type Patinet Name or BHT : "/>
                                    <p:autoComplete widgetVar="aPt2"  id="acPt2" forceSelection="true" converter="admis"
                                                    value="#{inwardServiceBillController.current.patientEncounter}" 
                                                    completeMethod="#{admissionController.completePatientBht}"
                                                    var="apt2" itemLabel="#{amp2.bhtNo}"
                                                    itemValue="#{apt2}" size="30"  style="width: 400px;">
                                        <p:ajax event="itemSelect" process="acPt2" update="panSearch2"/>
                                        <p:column >
                                            #{apt2.bhtNo} 
                                        </p:column>
                                        <p:column >
                                            #{apt2.patient.person.name} 
                                        </p:column>
                                    </p:autoComplete>  

                                    <br/><br/>
                                    <h:panelGrid id="panSearch2" columns="2" >
                                        <h:outputLabel  rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="BHT No : " />
                                        <h:outputLabel rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="#{inwardServiceBillController.current.patientEncounter.bhtNo}" ></h:outputLabel>


                                        <h:outputLabel  rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="Patient Name : " />
                                        <h:outputLabel rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="#{inwardServiceBillController.current.patientEncounter.patient.person.nameWithTitle}" ></h:outputLabel>

                                        <h:outputLabel  rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="Adress : " />
                                        <h:outputLabel rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="#{inwardServiceBillController.current.patientEncounter.patient.person.address}" ></h:outputLabel>

                                        <h:outputLabel  rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="Guardian Name : " />
                                        <h:outputLabel rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="#{inwardServiceBillController.current.patientEncounter.guardian.nameWithTitle}" ></h:outputLabel>

                                        <h:outputLabel  rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="Age : " />
                                        <h:outputLabel rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="#{inwardServiceBillController.current.patientEncounter.patient.age}" ></h:outputLabel>
                                        <h:outputLabel  rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="Admitted At : "  />
                                        <h:outputLabel rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="#{inwardServiceBillController.current.patientEncounter.dateOfAdmission}"   >
                                            <f:convertDateTime pattern="dd/MM/yy" />
                                        </h:outputLabel>
                                        <h:outputLabel  rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="Adress" />
                                        <h:outputLabel rendered="#{inwardServiceBillController.current.patientEncounter!=null}" value="#{inwardServiceBillController.current.patientEncounter.patient.person.address}" ></h:outputLabel>
                                    </h:panelGrid>
                                </p:panel>
                            </h:panelGroup>


                            <h:panelGroup id="panelIx" >


                                <p:panel id='pIxAdd' header="Service Requests" style="min-height: 200px;"  >
                                    <p:focus  context="acIx"/>
                                    <p:autoComplete widgetVar="aIx" id="acIx" forceSelection="true" value="#{inwardServiceBillController.currentBillItem.item}" completeMethod="#{serviceController.completeService}" var="ix" itemLabel="#{ix.name}" itemValue="#{ix}" size="30"  style="width: 400px;">
                                        <p:column>
                                            #{ix.name}
                                        </p:column>
                                        <p:column>
                                            #{ix.total}
                                        </p:column>
                                    </p:autoComplete>
                                    <h:commandButton id="btnAddIx" value="Add" action="#{inwardServiceBillController.addToBill()}">
                                        <f:ajax execute="acIx btnAddIx" render="focusIx tblBillItem  pBillDetails pIxAdd" />
                                    </h:commandButton>
                                    <p:defaultCommand target="btnAddIx" />
                                </p:panel>

                            </h:panelGroup>

                        </h:panelGrid>


                        <h:panelGrid columns="1" style="min-width: 400px;" >
                            <h:panelGroup id="panelBillItems"  >

                                <p:tabView id="tblBillItem" >
                                    <p:tab id="tabBillItems" title="Bill Items" >
                                        <p:dataTable id="tblRequests" rowIndexVar="rowIndex" value="#{inwardServiceBillController.lstBillEntries}" var="bi" >

                                            <p:column>
                                                <f:facet name="header">No</f:facet>
                                                #{rowIndex+1}
                                            </p:column>
                                            <p:column>
                                                <f:facet name="header">Item</f:facet>
                                                #{bi.billItem.item.name}
                                            </p:column>
                                            <p:column>
                                                <f:facet name="header">Fee</f:facet>
                                                #{bi.billItem.netValue}
                                            </p:column>
                                            <p:column>
                                                <h:commandButton id="btnRemove" value="X" action="#{inwardServiceBillController.removeBillItem}" >
                                                    <f:setPropertyActionListener  value="#{rowIndex}" target="#{inwardServiceBillController.index}" />
                                                    <p:ajax  process="btnRemove" update="@form" />
                                                </h:commandButton>
                                            </p:column>
                                        </p:dataTable>
                                    </p:tab>

                                    <p:tab id="tabBillFee" title="Fees" >
                                        <p:dataTable rowIndexVar="rowIndex" value="#{inwardServiceBillController.lstBillFees}" var="bif" >
                                            <p:column>
                                                <f:facet name="header">No</f:facet>
                                                #{rowIndex+1}
                                            </p:column>
                                            <p:column>
                                                <f:facet name="header">Fee</f:facet>
                                                <h:inputText autocomplete="off" id="txtFeeVal" value="#{bif.feeValue}" disabled="#{!bif.billItem.item.userChangable}" >
                                                    <f:ajax event="keyup" execute="txtFeeVal" render=" :#{p:component('tblRequests')}  :#{p:component('pBillDetails')}" listener="#{inwardServiceBillController.feeChanged}" />
                                                </h:inputText>
                                            </p:column>
                                            <p:column>
                                                <f:facet name="header">Payee</f:facet>
                                                
                                                <h:outputLabel value="#{bif.department.name}" rendered="#{bif.department!=null}" ></h:outputLabel>
                                                 <h:outputLabel value="#{bif.fee.name}"></h:outputLabel>
                                                <h:outputLabel value="#{bif.staff.person.name}" rendered="#{bif.staff!=null}" ></h:outputLabel>
                                            </p:column>
                                        </p:dataTable>
                                    </p:tab>
                                </p:tabView>



                            </h:panelGroup>

                            <h:panelGroup id="panelBillTotals" >

                                <p:panel header="Bill Details" id="pBillDetails" style="padding: 5px; margin: 10px;" >
                                    <p:panelGrid columns="2" columnClasses="numberCol, textCol"  >
                                        <h:outputLabel value="Total" ></h:outputLabel>
                                        <h:outputLabel id="tot" value="#{inwardServiceBillController.current.total}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                        </h:outputLabel>                                       
                                    </p:panelGrid>

                                </p:panel>
                            </h:panelGroup>




                        </h:panelGrid>


                    </h:panelGrid>

                </p:panel>


            </h:form>
        </h:panelGroup>
    </ui:define>
</ui:composition>
